package net.esj.basic.utils;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;

public final class UUIDCreator {

	public static String create(){
		return UUID.randomUUID().toString().replaceAll("-", "");
	}	
	
	/**
	 * 创建16位
	 * @return
	 */
	public static String createEncodeBase64(){
		byte[] bytes = new byte[16];
		UUID uuid = UUID.randomUUID();
		long least = uuid.getLeastSignificantBits();
		long most = uuid.getMostSignificantBits();
		long2Bytes(most, bytes, 0);
		long2Bytes(least, bytes, 8);
		return Base64.encodeBase64URLSafeString(bytes);
	}
	
	private static void long2Bytes(long value,byte[] bytes,int offset){
		for(int i=7;i>-1;i--){
			bytes[offset++] = (byte)((value>>8*i)&0xFF);
		}
	}
	
	private final static char[] B64= { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z','|','^'};
	
	public static void main(String[] args){
		System.out.println(createEncodeBase64());
		
		//String uuid = "FF808081-3369-F278-0133-85F7A3220AC5";
		String uuid = "FF8080813369F278013385F7A3220AC5";
		//long l1 = 65408;11e
		//long l2 = 32897;8
		
		int s = 0;
		StringBuilder bb = new StringBuilder("结果是：");
		for(int i=0;i<uuid.length();i=i+4){
			StringBuilder sb = new StringBuilder("0x");
			sb.append(uuid.charAt(i)).append(uuid.charAt(i+1)).append(uuid.charAt(i+2)).append(uuid.charAt(i+3));
			System.out.println(sb.toString());
			Long l = Long.decode(sb.toString());
			Long d1 = l/64;
			Long  d3 = l%64;
			Long d2 = 0L;
			if(d1>63){
				d2 = d1%64;
				d1 = d1/64;
			}
			 System.out.println(B64[d1.intValue()]+""+B64[d2.intValue()]+""+B64[d3.intValue()]);
			 bb.append(B64[d1.intValue()]+""+B64[d2.intValue()]+""+B64[d3.intValue()]);
			 s=0;
		}
		System.out.println(bb.toString());
		
//		65408
//		System.out.println(uuid.hashCode());
//		long d = Long.decode("0xff80");
//		System.out.println(d);
//		System.out.println(StringUtils.longToN62(d));
		//System.out.println(encodeBase64(uuid));
	}
}
